//
//  PYImageManager.h
//  IHome
//
//   Created by zhujizheng on 2020/2/18.
//  Copyright © 2019年 com.zhujizheng. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface PYImageManager : NSObject

/**
 获取图片在硬盘里的缓存大小
 
 @param block 格式化后的空间大小字符串，已经在主线程中回调了
 */
+ (void)getImageStoreSpaceWithFormatString:(void (^)(NSString *formatString))block;

/**
 清除硬盘所有图片
 
 @param completion 清除完成的回调，已经在主线程回调了
 */
+ (void)clearAllDiskImage:(void(^)(BOOL error))completion;

/**
 清除所有图片，包括硬盘和内存中的图片
 */
+ (void)clearAllImage;

@end

typedef NS_ENUM(NSInteger, PYImageType) {
    PYImageTypeNone = 0,
    PYImageTypeJPEG = 1,
    PYImageTypePNG = 2,
    PYImageTypeGIF = 3,
    PYImageTypeTIFF = 4,
    PYImageTypeWEBP = 5,
};

static inline PYImageType PYImageTypeFromData(NSData *imageData) {
    uint8_t c;
    [imageData getBytes:&c length:1];
    switch (c) {
        case 0xFF:
            return PYImageTypeJPEG;
        case 0x89:
            return PYImageTypePNG;
        case 0x47:
            return PYImageTypeGIF;
        case 0x49:
        case 0x4D:
            return PYImageTypeTIFF;
        case 0x52:
            if ([imageData length] < 12) {
                return PYImageTypeNone;
            }
            NSString *testString = [[NSString alloc] initWithData:[imageData subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
            if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {
                return PYImageTypeWEBP;
            }
            return PYImageTypeNone;
    }
    return PYImageTypeNone;
}

NS_ASSUME_NONNULL_END
